﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using LibService.DTO;
using System.Data;


namespace LibService.DAO
{
    public class TaiKhoanDAO
    {
        #region 1. Lay Danh Sach Thong Tin TaiKhoan
        public static List<TaiKhoanDTO> layDanhSachTK()
        {
            List<TaiKhoanDTO> lst = new List<TaiKhoanDTO>();
            try
            {
                //string pass = "******";
                SQLConnect cnn = new SQLConnect();
                DataTable dt = new DataTable();
                string sql = "select * from TaiKhoan";
                dt = cnn.ExecuteQuery(sql, CommandType.Text);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TaiKhoanDTO tk = new TaiKhoanDTO();
                    tk.MaTK = dt.Rows[i]["MaTK"].ToString();
                    tk.UserName = dt.Rows[i]["UserName"].ToString();
                    tk.PassWord = dt.Rows[i]["PassWord"].ToString();
                    tk.Email = dt.Rows[i]["Email"].ToString();
                    lst.Add(tk);
                }
            }
            catch (Exception ex)
            {

            }
            return lst;
        }
        #endregion

        #region 2. Lay thong tin Tai KHoan theo ma
        public static TaiKhoanDTO layThongTinTKTheoMa(String maTK)
        {
            TaiKhoanDTO tk = new TaiKhoanDTO();
            try
            {
                SQLConnect cnn = new SQLConnect();
                DataTable dt = new DataTable();
               //string pass = "******";
                string sql = "select * from TaiKhoan where MaTK='"+ maTK+"'";
                dt = cnn.ExecuteQuery(sql, CommandType.Text);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tk.MaTK = dt.Rows[i]["MaTK"].ToString();
                    tk.UserName = dt.Rows[i]["UserName"].ToString();
                    tk.PassWord = dt.Rows[i]["PassWord"].ToString();
                    tk.Email = dt.Rows[i]["Email"].ToString();
                }
            }
            catch (Exception ex) { }
            return tk;
        }
        #endregion

        #region 3. Them thong tin TK
        public static bool InsertTK(TaiKhoanDTO dv)
        {
            bool result = false;
            try
            {
                SQLConnect cnn = new SQLConnect();
                List<SqlParameter> sqlPar = new List<SqlParameter>();
                sqlPar.Add(new SqlParameter("@maTK", dv.MaTK));
                sqlPar.Add(new SqlParameter("@userName", dv.UserName));
                sqlPar.Add(new SqlParameter("@passWord", dv.PassWord));
                sqlPar.Add(new SqlParameter("@eMail", dv.Email));
                string sqlString = @"Insert into TaiKhoan values(@maTK, @userName, @passWord, @eMail)";
                int n = cnn.ExecuteNonQuery(sqlString, CommandType.Text, sqlPar);
                

                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex) { }
            return result;
        }
        #endregion
       // private static Boolean daDangNhap = false;
        public static bool Login(String userName, String passWord)
        {
            bool result = false;
            try
            {
                List<TaiKhoanDTO> DSTK = layDanhSachTK();
                for (int i = 0; i < DSTK.Count; i++)
                {
                    if (DSTK[i].UserName == userName && DSTK[i].PassWord == passWord)
                    {
                        return result = true;
                    }
                }
                //SQLConnect cnn = new SQLConnect();
                //List<SqlParameter> sqlPara = new List<SqlParameter>();
                ////sqlPara.Add(new SqlParameter("@User", userName));
                ////sqlPara.Add(new SqlParameter("@Pass", passWord));
                //string sql = "select * from TaiKhoan Where UserName = '"+userName+"' and PassWord = '"+passWord+"'";
                //int n = cnn.ExecuteNonQuery(sql, CommandType.Text);
                //if (n==1)
                //{
                //    return result= true;
                //}
                //return result= false;
            }
            catch (Exception)
            {
                
                throw;
            }
            return result;
        }

        public void Logout()
        {


        }
        

        //#region 4. Xoa thong tin cua TK
        //public static bool DeleteTK(String maTK)
        //{
        //    bool result = false;
        //    try
        //    {
        //        SQLConnect cnn = new SQLConnect();
        //        List<SqlParameter> sqlPara = new List<SqlParameter>();
        //        sqlPara.Add(new SqlParameter("@maTK", maTK));

        //        int n = cnn.ExecuteNonQuery("", CommandType.StoredProcedure, sqlPara);
        //        if (n == 1)
        //        {
        //            result = true;
        //        }
        //    }
        //    catch (Exception ex) { }
        //    return result;
        //}

        //#endregion

        //#region 5. Cap nhat thong tin 
        //public static bool UpdateTK(TaiKhoanDTO tk)
        //{
        //    bool result = false;
        //    try
        //    {

        //    }
        //    catch (Exception ex) { }
        //    return result;
        //}
        //#endregion
    }
}